﻿// 取 Grid 中的数据，拼写成 XML 格式
function getGridDataXML(ogrid)
{
	if (ogrid)
	{
		var xmlData = "<dataxml entity=\"p_cwjkKjkmMap\" keyname=\"\">";
		var iRowsLen = ogrid.rows.length;
		var oTr, oTd;
		var sCwztGUID,sBz,sKmtypeGUID,sOperObject,sData;
						
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			
			if (oTr.entity == "1") 
			{
				sCwztGUID = oTr.cwztguid;
			}
			else
			{
				sCwztGUID = document.all("txtCwztGUID").value;
			}
			
			if (oTr.entity == "1") 
			{
				sBz = oTr.bz;
			}
			else
			{
				sBz = document.all("txtBz").value;
			}
			
			if (oTr.entity == "1") 
			{
				sKmtypeGUID = oTr.kmtypeguid;
			}
			else
			{
				sKmtypeGUID = document.all("txtKmTypeGUID").value;
			}
			
			if (oTr.entity == "1") 
			{
				sOperObject = oTr.operobject;
			}
			else
			{
				sOperObject = document.all("txtOperObject").value;
			}
					
			xmlData += "<row oid=\"" + oTr.oid + "\"" + " objectguid=\"" + oTr.objectguid + "\""
				    + " buguid=\"" + oTr.buguid + "\"" + " entity=\"" + oTr.entity + "\"" + " cwztguid=\""
				    + sCwztGUID + "\"" + " bz=\"" + sBz + "\"" + " kmtypeguid=\"" + sKmtypeGUID 
				    + "\"" + " operobject=\"" + sOperObject + "\">";
			
			// 不取第一列和最后一列的值
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];
				
				if (oTd.firstChild&&oTd.firstChild.value)
				{
					sData = Trim(oTd.firstChild.value);
				}
				else
				{
					sData = Trim(oTd.innerText);
				}
				
				xmlData += "<" + oTd.name + ">" + sData + "</" + oTd.name + ">";
			}

			xmlData += "</row>";
		}

		xmlData += "</dataxml>";
		
		return xmlData;
	}
}

//校验款项明细是否存在有效的记录（编辑的情况下）
function verifyGridRow(sXML)
{
	var xmlDom = new ActiveXObject("Microsoft.XMLDOM");
	xmlDom.loadXML(sXML);
	var Nodes = xmlDom.documentElement.selectNodes("//dataxml/row");
	
	if (Nodes.length>0)
	{
		return true;
	}
	
	return false;
}

//批量录入
function batchInput()
{
        var sMyarray,sFile,sParams;	
        
	    if(document.all("txtOperObject").value=="")
	    {
	       alert("请选择辅助核算！")
	       return;	
	    }
	    
		sParams = "&openroot=" + escape(document.all("txtOpenRoot").value);
		sParams += "&operobject=" + escape(document.all("txtOperObject").value);

    	var sTitle,sHeight,sWidth,sFile,sReturn;
		
		sTitle = "批量录入";
		sHeight = "200";
		sWidth = "350";
		sFile = "/PubProject/CWJK/Cwjk_BatchInput.aspx";		
						
		sReturn = openMyDlg(sTitle,sFile,sParams,"",sWidth,sHeight);
	    
		if ( sReturn != undefined && sReturn != "")
		{
			//页面值替换
			if ( gridBar )
			{				
				replaceGridData(gridBar,sReturn);				
			}
		}

}

//生成替换
function replaceGridData(ogrid,srptdata)
{
	if (ogrid)
	{
		var sNowData = "";
		var iRowsLen = ogrid.rows.length;
		var oTr,oTd,sNowCode,sNowName,sRe;
		var myArray = srptdata.split("!");	
			
		//debugger;
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			// 不取第一列和最后一列的值
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];			
				
				//开始替换								
				if (oTd.name == "kmcode") 
				{
					if (myArray[0] == "ALL")
					{
						if(oTd.firstChild&&oTd.firstChild.value) 
						{
							oTd.firstChild.value = myArray[1];
						}
						else
						{
							oTd.innerText =  myArray[1];
						}
					}
					else
					{
						if(oTd.firstChild&&oTd.firstChild.value) 
						{
							if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value =  myArray[1];
						}
						else
						{
							if(Trim(oTd.innerText) == "") oTd.innerText =  myArray[1];
						}
					}				
				}
				
				if (oTd.name == "kmname") 
				{
				   if (myArray[0] == "ALL")
					{
						if(oTd.firstChild&&oTd.firstChild.value) 
						{
							oTd.firstChild.value =  myArray[2];
						}
						else
						{
							oTd.innerText =  myArray[2];
						}
					}
					else
					{
						if(oTd.firstChild&&oTd.firstChild.value) 
						{
							if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value =  myArray[2];
						}
						else
						{
							if(Trim(oTd.innerText) == "") oTd.innerText = myArray[2];
						}
					}					
				}
			}
		}
	}
}

//导出辅助核算
function ExportKjkmDetail()
{   
    var sGetType = "KJKM";
    var sCwztGUID = document.all("txtCwztGUID").value;
    var sProjGUID = "";
    if(parent.document.all("txtProjGUID"))
    {
        sProjGUID = parent.document.all("txtProjGUID").value;
        if(sProjGUID.length!=36)
        {
            sProjGUID = parent.document.all("showProj").contentWindow.document.all("Project").value;
        }    
    }
    var sOperObject = document.all("txtOperObject").value;
    var sKmTypeGUID = document.all("txtKmTypeGUID").value;
    var sBz="";
    if(document.all("txtBz"))
    {
        sBz=document.all("txtBz").value;
    }
    var sApplication = document.all("txtApplication").value;
    
    var sPath = "/PubProject/Cwjk/Cwjk_XMLHTTP.aspx?Application="+escape(sApplication);
    sPath+="&GetType="+escape(sGetType);
    sPath+="&CwztGUID="+escape(sCwztGUID);
     //如果是房间的时候，直接传递楼栋guid过去
    if(sOperObject=="房间")
    {
       var arr=document.all("__projguid").value.split(";");
       if(arr.length>1)
       {
          sProjGUID=arr[1];       
       }
       
       if(!sProjGUID||(sProjGUID&&sProjGUID.length!=36))
       {
          sProjGUID="00000000-0000-0000-0000-000000000000" ;   
       }
    }
    sPath+="&ProjGUID="+escape(sProjGUID);
    sPath+="&OperObject="+escape(sOperObject);
    sPath+="&Bz="+escape(sBz);
    sPath+="&KmTypeGUID="+escape(sKmTypeGUID);
    
    strReturn = GetDataFromXMLHTTP(sPath,"ExportKjkmOrHsxmMap","","");
    
    if(strReturn!="")
    {
        //aobo edit by 2010-07-10 将下载的文件名与保存的文件名分开处理，原因为保存的文件名为固定，而存在服务的文件名随机产生
        //var sExportFileName = strReturn.substr(strReturn.lastIndexOf("\\") + 1);
        var sExportFileName = strReturn.substr(strReturn.lastIndexOf("|") + 1);
        var iTempFiles= strReturn.indexOf("\\TempFiles");
        var j= strReturn.lastIndexOf("|");
        var sFilePath = strReturn.substr(iTempFiles,j-iTempFiles);

        OpenDownloadWin(sExportFileName, sFilePath, "", "");
        /*
        var sFile = "Cwjk_DownLoad.aspx?filename="+ escape(strReturn)+ "&random="+Math.random();
        var iTop,iLeft;
        iTop = (screen.height - 200)/2;
        iLeft = (screen.width - 400)/2;
		window.open(sFile, "", "width=1px,height=1px,left="+ iLeft +"px,top="+ iTop +"px,status=0,resizable=1,menubar=yes");
		*/
    }
}
	
//导入会计科目对照信息
function ImportKjkmDetail()
{
    var sGetType = "KJKM";
    var sCwztGUID = document.all("txtCwztGUID").value;
    var sOperObject = document.all("txtOperObject").value;
    var sKmTypeGUID = document.all("txtKmTypeGUID").value;
    
    var sTitle, sHeight, sFile, sParams;
    var iWidth, iHeight;

    sTitle = "选择文件";
    sFile = "/PubProject/Cwjk/Cwjk_Import.aspx";
    sWidth = "680";
    sHeight = "480";
    
    // 后缀参数
    var sParams = "";
    sParams+= "GetType=" + escape(sGetType);
    sParams+= "&CwztGUID=" + escape(sCwztGUID);
    sParams+= "&OperObject=" + escape(sOperObject);
    sParams+= "&KmTypeGUID=" + escape(sKmTypeGUID);
    
    var url = "/FrameTemp0.aspx";
    url += "?title=" + escape(sTitle);
    url += "&filename=" + escape(sFile);
    url += "&param=" + escape(sParams);
	
    var sReturnValue = window.showModalDialog(url, "", "dialogWidth:" + sWidth + "px; dialogHeight:" + sHeight + "px; status:no; help:no; resizable:no;scroll:no;");
		
    if(sReturnValue=="OK")
    {
       alert("更新数据成功");
       try
       {
        document.all("txtPinYinCale").value = "SAVEOK";
       }
       catch(e)
       {
       }
       __doPostBack("","");
    }
    else
    {
        
    }
}	

//生成
function batchSet()
{	
	var sMyarray,sFile,sParams;
	
	if (__projguid && __projguid.value != "" && __projguid.value!="null")
	{
		sParams = "&openroot=" + escape(document.all("txtOpenRoot").value);
		sParams += "&operobject=" + escape(document.all("txtOperObject").value);

		switch (document.all("txtOperObject").value)
		{
			case "房间":
				sMyarray = __projguid.value.split(";");
				sParams += "&sguid=" + escape(sMyarray[1]);
				break;
			
			case "楼栋":
				sParams += "&sguid=" + escape(__projguid.value);
				break;
		}
		
		var sTitle,sHeight,sWidth,sFile,sReturn;
		
		sTitle = "批量生成";
		sHeight = "380";
		sWidth = "350";
		sFile = "/PubProject/CWJK/Cwjk_BatchSet.aspx";			
				
		sReturn = openMyDlg(sTitle,sFile,sParams,"",sWidth,sHeight);
	
		if ( sReturn != undefined && sReturn != "")
		{
			//页面值替换
			if ( gridBar )
			{
				if (document.all("txtOperObject").value != "预约单") 
				{
					rptGridData(gridBar,sReturn);
				}
				else
				{
					//预约单特殊处理
					var myArray = sReturn.split("!");	
					if (myArray[2]) myType = myArray[2].split(":");
					if (myArray[3]) myRule = myArray[3].split(":");
					
					document.all("txtHsType").value = "";
					document.all("txtRptRule").value = "";					
					
					if (myType && myType.length>1) document.all("txtHsType").value = myType[1];
					if (myRule && myRule.length>1) document.all("txtRptRule").value = myRule[1];
					
					__doPostBack("","");
				}
			}
		}
	}
	else
	{
		return;
	}
}

//生成替换
function rptGridData(ogrid,srptdata)
{
	if (ogrid)
	{
		var sNowData = "";
		var iRowsLen = ogrid.rows.length;
		var oTr,oTd,sNowCode,sNowName,sRe;
		var myArray = srptdata.split("!");	
		var myCode,myName,myType;
		if (myArray[0]) myCode = myArray[0].split(":");
		if (myArray[1]) myName = myArray[1].split(":");
		if (myArray[2]) myType = myArray[2].split(":");
		if (myArray[3]) myRule = myArray[3].split(":");
		
		var oldCodeLen=myCode[1].length;
		var oldNameLen=myName[1].length;
		 
		//debugger;
		for ( var i = 1; i < iRowsLen; i++)
		{
			oTr = ogrid.rows[i];
			// 不取第一列和最后一列的值
			for ( var j = 1; j < ogrid.rows[i].cells.length ; j++)
			{
				oTd = ogrid.rows[i].cells[j];
				
				//保存当前对象代码的原始值
				if (oTd.name == "objectcode") 
				{
					if (oTd.firstChild&&oTd.firstChild.value)
					{
						sNowCode = oTd.firstChild.value;
					}
					else
					{
						sNowCode = oTd.innerText;
					}
				}
				
				//保存当前对象名称的原始值
				if (oTd.name == "objectname") 
				{
					if (oTd.firstChild&&oTd.firstChild.value)
					{
						sNowName = oTd.firstChild.value;
					}
					else
					{
						sNowName = oTd.innerText;
					}
				}
				
				//开始替换								
				if (oTd.name == "kmcode") 
				{
					if (sNowCode !="" && myCode && myCode.length > 1) 
					{
						if (myRule[1] == "ALL")
						{
							if(oTd.firstChild&&oTd.firstChild.value) 
							{
								oTd.firstChild.value = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
							else
							{
								oTd.innerText = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
						}
						else
						{
							if(oTd.firstChild&&oTd.firstChild.value) 
							{
								if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
							else
							{
								if(Trim(oTd.innerText) == "") oTd.innerText = (myCode[2]+sNowCode.substring(oldCodeLen)).replace(/-/g,".");
							}
						}
					}
				}
				
				if (oTd.name == "kmname" && myName && myName.length > 1) 
				{
					if (sNowName !="") 
					{
						if (myRule[1] == "ALL")
						{
							if(oTd.firstChild&&oTd.firstChild.value) 
							{
								oTd.firstChild.value = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
							else
							{
								oTd.innerText =(myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
						}
						else
						{
							if(oTd.firstChild&&oTd.firstChild.value) 
							{
								if(Trim(oTd.firstChild.value) == "") oTd.firstChild.value = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
							else
							{
								if(Trim(oTd.innerText) == "") oTd.innerText = (myName[2]+sNowName.substring(oldNameLen)).replace(/-/g,"");
							}
						}
					}
				}
			}
		}
	}
}


function BeForeExportKjkmDetail()
{
   document.all.txtIsExport.value="Export";
   saveGridData();
}